// @file      Renderer.groovy
// @author    Mac Radigan
// @purpose   render YCOA web interface

package ycoa.markup

import sys.xml.XMLUtilities

def class Renderer {

  private String username
  private String fullname
  private String root = System.getenv("YCOA_WEB_HOME")

  public Renderer(username) {
    this.username = username
    this.fullname = username
  } // end of constructor

  public String render(body) {
    def sb = new StringBuffer()
    sb.append(getHeader())
    sb.append(body)
    sb.append(getMenu())
    sb.append(getFooter())
    return sb.toString()
  }

  public String renderLogin() {
    return render(getLogin())
  }

  public String getHeader() {
    def str
    def sb = new StringBuilder()

str="""
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Young Champions of America Intranet</title>
    <link rel="stylesheet" href="res/style/IEstyle.css" type="text/css"/>
    <script language="javascript" src="res/scripts/legacy.js" type="text/javascript"></script>
  </head>

  <body id="yahoo-com" class="BodyBG">

<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<!-- Header -->
<TR valign="top"><TD>
<div name="PageHeader" id="PageHeader">
<table width="100%" height="20" border="0" cellspacing="0" cellpadding="0" bgcolor="#CC6700" class="header">
	<TR valign="top">
		<TD>
			<table width="100%" border="0" cellspacing="0" cellpadding="0" height="56">
				<TR bgcolor="#8D0102">
						<TD><img src="res/images/YOLogo.jpg" alt="Young Champions of America" hspace="0" vspace="0" border="0"><BR></TD>
					
						<td valign="top">
"""
sb.append(str)

if(username) {
      str="""
							<span class="legend">user:</span>&nbsp;<font color="#3366cc"><strong>${username}</strong></font>&nbsp;&nbsp;</span> 
							<br>
							<span class="legend">(If you're not ${fullname}, <a href="login.glet?logout=true" class="greentext">click here</a>.)</span>
"""
    sb.append(str)
  }

str="""
						</td>
											
					
				</TR>
			</Table>
		</TD>
	</TR>
	<TR>
		<TD>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" height="20" class="headerLinkRow">
			<TR>
				<TD width=25%>
						<!-- MAC NOTE: place links of class headerlink here -->
				</TD>
				<TD align="right" width=45%>
					<!-- MAC NOTE: place links of class headerlink here -->
					&nbsp;&nbsp;<a href="login.glet?logout=true" class="headerlink">Log Out</a>&nbsp;&nbsp;
				</TD>
			</TR>
		</TABLE>		
		</TD>
	</TR>
</TABLE>
</div>


<div name="PrintHeader" id="PrintHeader" style="visibility:hidden;display:none">

	<table width="100%" align="center" cellpading="0" cellspacing="0" class="BodyBG">

		<tr valign="top">
			<td width="40%" nowrap>&nbsp;&nbsp;&nbsp;YCOA</td>	
			<td align="center" nowrap><a id="PageHeadline" class="BodyHeaderTitle2" href="javascript: printPage(0);" title="click here to return to normal mode"></a></td>
			<td width="40%" align="right" nowrap>7/26/2009,&nbsp;12:21:01 PM&nbsp;&nbsp;&nbsp;</td>
		</tr>

	</table>

</div>

</TD></TR>
"""
sb.append(str)

  } // end of getHeader()

  public String getFooter() {
return """
<!-- Footer -->
<TR valign="bottom"><TD>


<div name="PageFooter" id="PageFooter">
<table width="100%" height="20" border="0" cellspacing="10" cellpadding="0" class="footer" bgcolor="#6487DC">
	<TR class="footerLinkRow2">
		<TD nowrap>Arizona&nbsp;&nbsp;</TD>
		<TD align="right">© 2009&nbsp;Young Champions of America. All rights reserved.&nbsp;&nbsp;Powered by <a href="http://www.meiotic.com" target="_blank" class="footerlink">Meiotic Inc.</a></TD>
		<TD width="10">&nbsp;&nbsp;</TD>
	</TR>
</TABLE>
</div>
</TD></TR>

</table>
</body>
</html>
"""
  } // end of getFooter()

  public String getLogin() {
return """
<!-- Main Content -->
<TR height="100%"><TD valign="top" align="center">
	
<html>
<head>
	<title>Young Champions of America</title>
	<script language="JavaScript">
	
	
	function setFocus() 
	{
		document.login.login_username.focus();
	}
	
	function ValidateUserInput()
	{
		var validLogin = (document.login.login_username.value.length > 0) && (document.login.login_password.value.length >= 4)
		if (validLogin) return true;
		else
		{
			alert("You must enter a username and a password");
			return false;
		}
	}
	
	function displayMessage()
	{
		window.location.replace('index.asp?v1=message.asp&n=201');
	}
	</script>
</head>
<body onload="setFocus();">	

	
		<table>
			<tr><td align="center">&nbsp;</td></tr>
		</table>
	
	
	<form action="login.glet" method="post" name="login" id="login">
		<table cellpadding="0" cellspacing="0" border="0" class="loginBoxBorder">
		
			<tr class="loginHeader">
				<td>
				<!-- Login Box Top Portion -->
				<!-- Title Bar -->
				<table cellpadding="0" cellspacing="0" border="0" width="362" height="20">
				    <tr>
						<td class="loginTitle">&nbsp;Please Login</td>		
					</tr>
				</table>
				<!-- End Title Bar -->
				<!-- End Login Box Top Portion -->
				<!-- Login Box Middle Portion -->
				<table cellpadding="0" cellspacing="0" border="0" width="362">
					<tr>
						<td width="346" height="20"><img src="res/images/trans.gif" width="346" height="20" border="0"></td>
					</tr>
					<tr>
						<td align="center" valign="top">
							<table border="0" cellspacing="0" cellpadding="0">
								<TR><TD colspan="3">
								 
								<BR><br></TD></TR>
								<TR>
									<TD>username</TD><TD>&nbsp;&nbsp;&nbsp;</TD><TD><input type="text" class="textbox" name="username" maxlength="12" size="12"></TD>							
								</TR>
								<TR>
									<TD><img src="res/images/trans.gif" height="10" border="0"></TD>
								</TR>
								<TR>
									<TD>password</TD><TD>&nbsp;&nbsp;&nbsp;</TD><TD><input type="password" class="textbox" name="password" maxlength="12" size="12"></TD>
								</TR>
								<TR>
									<TD>database</TD><TD>&nbsp;&nbsp;&nbsp;</TD>
                                                                        <TD>
                                                                          <select name="database">
                                                                            <option name="test">Test</option>
                                                                            <option name="az">Arizona</option>
                                                                            <option name="kc">Kansas</option>
                                                                          </select>
                                                                        </TD>							
								</TR>
								
							</TABLE>
						</td>
					</tr>
					<tr>
						<td width="346" height="20"><img src="res/images/trans.gif" width="346" height="20" border="0"></td>
					</tr>			
				</table>
				<!-- End Login Box Middle Portion -->
				<!-- Login Box Bottom Portion -->
				<table cellpadding="0" cellspacing="0" border="0" width="362" height="23">
					<tr>
						<td width="163" height="23"valign="top"><input type="Image" onclick="javascript:if (ValidateUserInput())document.login.submit();" src="res/images/login_unselected.gif" alt="login" width="163" height="23" border="0" onMouseOver="this.src='res/images/login_selected.gif';this.style.cursor='hand';" onMouseOut="this.src='res/images/login_unselected.gif';"></td>						
					</tr>
				</table>
				<!-- End Login Box Bottom Portion -->
				</td>
			</tr>
		</table>
		</form>
		
  
	
	<table align="center">		
		<tr>
   			<TD><IMG SRC="res/images/spacers/spacer.trans.gif" WIDTH="1" HEIGHT="10" BORDER="0" ALT=""></TD>		
 		</tr>
		 <tr>
			<TD>
				<table WIDTH="500" border="0" cellspacing="0" cellpadding="5" CLASS="MSGFormatTable">
					<TR>
						<TD VALIGN="TOP" CLASS="MSGFormatBody">
							<b>** Attention ** UPDATED 10/12</b><br>
The application is undergoing enhancements.<br>
<b>If you  have any issues please contact Kraig Hollingworth - ASAP </b><br><br>
If you have any questions please direct them to <a href="mailto:kraig@ycoaoffice.com">Kraig Hollingworth</a> or to <a href="mailto:ppacun@tradebonds.com">Paul Pacun</a>.
<br><br>Thank You
	
						</TD>
					</TR>
				</TABLE>
			</TD>
		</TR>
		<tr>
			<TD><IMG SRC="res/images/spacers/spacer.trans.gif" WIDTH="1" HEIGHT="10" BORDER="0" ALT=""></TD>		
		</tr>
	</table>
		

</body>
</html>

	
</td></TR>
"""
  } // end of getLogin()

  public String getMenu() {
    def xmlFile = "$root/res/xml/office-1.menu.xml"
    def xslFile = "$root/res/xslt/menu.context.xslt.xml"
    def xml = XMLUtilities.newDocument( new File(xmlFile).text )
    def xsl = XMLUtilities.newDocument( new File(xslFile).text )
    def xmlResult = XMLUtilities.xslt(xml, xsl)
    return XMLUtilities.parseToString(xmlResult)
  } // end of getMenu()

} // end of class Renderer
